package com.briup.product_my.service.impl;

import com.briup.product_my.domain.Analysis;
import com.briup.product_my.mapper.*;
import com.briup.product_my.service.AnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @Auther: xiongwenbin
 * @Date: 2024/8/23-08-23-下午2:33
 * @Description: com.briup.product_my.service.impl
 */
@Service
public class AnalysisServiceImpl implements AnalysisService {
    @Autowired
    private ManagerFenceHouseMapper managerFenceHouseMapper;
    @Autowired
    private ManagerHurdlesMapper managerHurdlesMapper;
    @Autowired
    private ManagerAnimalMapper managerAnimalMapper;
    @Autowired
    private ManagerFreezerMapper managerFreezerMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private IssueRecordMapper issueRecordMapper;
    @Override
    public Analysis count() {
        Analysis analysis = new Analysis();
        List<String> list =new ArrayList<>();
        list.add("栏舍");
        list.add("栏圈");
        list.add("动物");
        list.add("冷库");
        list.add("员工");
        List<Integer> countList = new ArrayList<>();
        countList.add(managerFenceHouseMapper.count());
        countList.add(managerHurdlesMapper.count());
        countList.add(managerAnimalMapper.count());
        countList.add(managerFreezerMapper.count());
        countList.add(accountMapper.count());
        analysis.setName(list);
        analysis.setValue(countList);
        return analysis;
    }

    @Override
    public Object indexCount() {
        Map<String,Integer> map = new HashMap<>();
        map.put("30以下",managerAnimalMapper.countByWeigth(0,30));
        map.put("50以上",managerAnimalMapper.countByWeigth(50,999999999));
        map.put("30~50",managerAnimalMapper.countByWeigth(30,50));
        return map;
    }

    @Override
    public Analysis countSales() {
        // 统计月度销售数据
        Analysis analysis = new Analysis();
        List<String> list =new ArrayList<>();
        list.add("1月");
        list.add("2月");
        list.add("3月");
        list.add("4月");
        list.add("5月");
        list.add("6月");
        list.add("7月");
        list.add("8月");
        list.add("9月");
        list.add("10月");
        list.add("11月");
        list.add("12月");
        List<Integer> countList = new ArrayList<>();

        list.forEach(s -> {
            if(!s.equals("12月")){
                LocalDate date = LocalDate.of(2023, Integer.parseInt(s.substring(0,s.indexOf("月"))) , 1);
                LocalDate endDate = LocalDate.of(2023, Integer.parseInt(s.substring(0,s.indexOf("月")))+1 , 1);
                countList.add(issueRecordMapper.countByMonth(date, endDate));
            }else{
                LocalDate date = LocalDate.of(2023, 12, 1);
                LocalDate endDate = LocalDate.of(2024, 1, 1);
                countList.add(issueRecordMapper.countByMonth(date, endDate));
            }

        });
        analysis.setName(list);
        analysis.setValue(countList);
        return analysis;
    }
}
